home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap08 / Beeper2 / Beeper2.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  2.7 KB  |  91 lines

  1. /*----------------------------------------
  2.    BEEPER2.C -- Timer Demo Program No. 2
  3.                 (c) Charles Petzold, 1998
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define ID_TIMER    1
  9.  
  10. LRESULT CALLBACK WndProc   (HWND, UINT, WPARAM, LPARAM) ;
  11. VOID    CALLBACK TimerProc (HWND, UINT, UINT,   DWORD ) ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15. {
  16.      static TCHAR szAppName[] = TEXT ("Beeper2") ;
  17.      HWND         hwnd ;
  18.      MSG          msg ;
  19.      WNDCLASS     wndclass ;
  20.      
  21.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.      wndclass.lpfnWndProc   = WndProc ;
  23.      wndclass.cbClsExtra    = 0 ;
  24.      wndclass.cbWndExtra    = 0 ;
  25.      wndclass.hInstance     = hInstance ;
  26.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  29.      wndclass.lpszMenuName  = NULL ;
  30.      wndclass.lpszClassName = szAppName ;
  31.      
  32.      if (!RegisterClass (&wndclass))
  33.      {
  34.           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
  35.                       szAppName, MB_ICONERROR) ;
  36.           return 0 ;
  37.      }
  38.      
  39.      hwnd = CreateWindow (szAppName, TEXT ("Beeper2 Timer Demo"),
  40.                           WS_OVERLAPPEDWINDOW,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           NULL, NULL, hInstance, NULL) ;
  44.      
  45.      ShowWindow (hwnd, iCmdShow) ;
  46.      UpdateWindow (hwnd) ;
  47.           
  48.      while (GetMessage (&msg, NULL, 0, 0))
  49.      {
  50.           TranslateMessage (&msg) ;
  51.           DispatchMessage (&msg) ;
  52.      }
  53.      return msg.wParam ;
  54. }
  55.  
  56. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  57. {
  58.      switch (message)
  59.      {
  60.      case WM_CREATE:
  61.           SetTimer (hwnd, ID_TIMER, 1000, TimerProc) ;
  62.           return 0 ;
  63.           
  64.      case WM_DESTROY:
  65.           KillTimer (hwnd, ID_TIMER) ;
  66.           PostQuitMessage (0) ;
  67.           return 0 ;
  68.      }
  69.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  70. }
  71.  
  72. VOID CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
  73. {
  74.      static BOOL fFlipFlop = FALSE ;
  75.      HBRUSH      hBrush ;
  76.      HDC         hdc ;
  77.      RECT        rc ;
  78.      
  79.      MessageBeep (-1) ;
  80.      fFlipFlop = !fFlipFlop ;
  81.      
  82.      GetClientRect (hwnd, &rc) ;
  83.      
  84.      hdc = GetDC (hwnd) ;
  85.      hBrush = CreateSolidBrush (fFlipFlop ? RGB(255,0,0) : RGB(0,0,255)) ;
  86.      
  87.      FillRect (hdc, &rc, hBrush) ;
  88.      ReleaseDC (hwnd, hdc) ;
  89.      DeleteObject (hBrush) ;
  90. }
  91.